/**
 * Copyright 2013-2015
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */	  

package com.cos.controller.app.appuser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cos.controller.app.model.AppGroup;
import com.cos.controller.app.model.AppUser;
import com.cos.controller.app.model.CompanyNote;
import com.cos.controller.app.model.NoteGroup;
import com.cos.controller.app.util.DepartmentUtil;
import com.cos.controller.base.BaseController;
import com.cos.service.system.appuser.AppuserService;
import com.cos.util.AppUtil;
import com.cos.util.PageData;
import com.cos.util.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * APP的接口
 * <p/>
 * 1.获取通讯录下的用户组及用户
 * 2.获取公司录
 * 3.
 * @author <a href="mailto:978954852@qq.com">xfjin</a>
 * @version  Date: 2016年3月24日 上午8:50:18
 * @serial 1.0
 * @since 2016年3月24日 上午8:50:18
 */
@Controller
@RequestMapping(value="/appuser")
public class IntNoteController extends BaseController{
	@Resource(name="appuserService")
	private AppuserService appuserService;
	
	/**
	 * 获取通讯录下的用户组及用户
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getNoteGroup")
	@ResponseBody
	private String getNoteGroup(HttpServletResponse response) throws Exception{
		logBefore(logger, "根据用户ID获得用户群组");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		JSONObject group=new JSONObject();
		List<NoteGroup> appGroupList = new ArrayList<NoteGroup>();
		pd = this.getPageData();
		String result = "00";
		try{
			if(AppUtil.checkParam("getGroup", pd)){	//检查参数
				List<PageData> groupList = new ArrayList<PageData>();
				List<PageData> userList = new ArrayList<PageData>();
			
				groupList = appuserService.getNoteGroup(pd);//根据用户ID取出该用户的组
				
				for(int i=0;i<groupList.size();i++){
					NoteGroup noteGroup = new NoteGroup();
					String Group_ID = (String) groupList.get(i).get("GROUP_ID");
					userList = appuserService.getUserByNoteGroupID(Group_ID);//根据该用户的组取出来下面的用户
					noteGroup.setGROUP_ID(Group_ID);
					noteGroup.setGROUP_NAME(groupList.get(i).getString("GROUP_NAME"));
					noteGroup.setUserList(userList);
					appGroupList.add(noteGroup);
				}
				result = (null == pd) ?  "02" :  "01";
			}else {
				result = "03";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			group.put("result", result);
			logAfter(logger);
		}
		JSONArray rows = JSONArray.fromObject(appGroupList);
		group.put("rows", rows);
		ResponseUtil.write(response, group);
		return null;
	}
	
	/**
	 * 获取公司录
	 * @param response    公司ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getNoteCompany")
	@ResponseBody
	public String getNoteCompany(HttpServletResponse response) throws Exception{
		logBefore(logger, "根据用户ID获取");
		PageData pd = new PageData();
		JSONObject group=new JSONObject();
		JSONArray rows = new JSONArray();
		List<CompanyNote> companyNoteList = new ArrayList<CompanyNote>(); //所有父类部门的实体对象
	//	List<CompanyNote> parentList = new ArrayList<CompanyNote>();
		pd = this.getPageData();
		String result = "00";
		try{
			if(AppUtil.checkParam("getDepartment", pd)){	//检查参数
				List<PageData> parentDepartmentList = new ArrayList<PageData>();//父类部门
				List<PageData> departmentList = new ArrayList<PageData>();//子类部门
				List<PageData> userList = new ArrayList<PageData>();//部门下的用户
				parentDepartmentList = appuserService.getDepartmentParent(pd);//先取出父类部门
				for(int i=0;i<parentDepartmentList.size();i++){//第一次循环 如果有子类部门循环部门去取出相关信息
					CompanyNote companyNote = new CompanyNote();//父类部门的实体对象
					String parentId = (String) parentDepartmentList.get(i).get("ID");
					companyNote.setID((String) parentDepartmentList.get(i).get("ID"));
					companyNote.setNAME(parentDepartmentList.get(i).getString("NAME")); 
					departmentList = appuserService.getDepartmentByParentID(parentId);
					if(departmentList.size()>=1){//判断是否有子类部门  如果有那就取出来
						List<CompanyNote> list = new ArrayList<CompanyNote>();
						for(int j=0;j<departmentList.size();j++){ //第二次循环 读出子类部门
							CompanyNote company = new CompanyNote();//子类部门的实体对象
							List<AppUser> appUserList  = new ArrayList<AppUser>();
							userList = appuserService.getUserListByDepartmentID((String) departmentList.get(j).get("ID"));
							for(int k=0;k<userList.size();k++){  //第三次循环如果有用户 就取出部门下的用户
								AppUser appUser =  new AppUser();
								appUser.setUSER_ID((String) userList.get(k).get("USER_ID"));
								appUser.setNAME((String) userList.get(k).get("NAME"));
								appUser.setPHONE((String) userList.get(k).get("PHONE"));
								appUser.setIMG_URL_BIG((String) userList.get(k).get("IMG_URL_BIG"));
								appUserList.add(appUser);
							}
							company.setID((String) departmentList.get(j).get("ID"));
							company.setNAME((String) departmentList.get(j).get("NAME"));
							company.setPARENT_ID((String) departmentList.get(j).get("PARENT_ID"));
							company.setUserList(appUserList);//用户已经加入
							list.add(company);
							
						}
						companyNote.setDepartmentList(list);//把封装好的子类部门的数据放到父类部门实体中
						companyNoteList.add(companyNote);
						
					}else{//如果没有就取出该父类部门下的用户
						List<AppUser> appUserList  = new ArrayList<AppUser>();
						userList = appuserService.getUserListByDepartmentID( (String) parentDepartmentList.get(i).get("ID"));
						for(int k=0;k<userList.size();k++){  //循环如果有用户 就取出部门下的用户
							AppUser appUser =  new AppUser();
							appUser.setUSER_ID((String) userList.get(k).get("USER_ID"));
							appUser.setNAME((String) userList.get(k).get("NAME"));
							appUser.setPHONE((String) userList.get(k).get("PHONE"));
							appUser.setIMG_URL_BIG((String) userList.get(k).get("IMG_URL_BIG"));
							appUserList.add(appUser);
						}
						companyNote.setUserList(appUserList);//用户已经加入
						companyNoteList.add(companyNote);
					}
				}
				result = (null == parentDepartmentList) ?  "02" :  "01";
				
				rows = JSONArray.fromObject(companyNoteList);
			}else {
				result = "03";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			group.put("result", result);
			logAfter(logger);
		}
		
		group.put("rows", rows);
		ResponseUtil.write(response, group);
		return null;
	}
	
}
